// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Market.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Using Directives

    using System;

    using Org.OpenTrader.Framework.Entities;
    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The market.
    /// </summary>
    [Serializable]
    public class Market : IMarket
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Market"/> class.
        /// </summary>
        /// <param name="dbMarket">
        /// The dbMarket.
        /// </param>
        public Market(DBMarket dbMarket)
        {
            this.ID = dbMarket.ID;
            this.Path = dbMarket.Path;

            // Name = dbMarket.Name;

            // PreOpeningStart = (DateTime)dbMarket.PreOpeningStart;
            // PreOpeningStop = (DateTime)dbMarket.PreOpeningStop;

            // OpeningStart = (DateTime)dbMarket.OpeningStart;
            // OpeningStop = (DateTime)dbMarket.OpeningStop;

            // ContinuousTradingStart = (DateTime)dbMarket.ContinuousTradingStart;
            // ContinuousTradingStop = (DateTime)dbMarket.ContinuousTradingStop;

            // IntradayAuctionStart = (DateTime)dbMarket.IntradayAuctionStart;
            // IntradayAuctionStop = (DateTime)dbMarket.IntradayAuctionStop;

            // PreClosingStart = (DateTime)dbMarket.PreClosingStart;
            // PreClosingStop = (DateTime)dbMarket.PreClosingStop;

            // ClosingStart = (DateTime)dbMarket.ClosingStart;
            // ClosingStop = (DateTime)dbMarket.ClosingStop;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets ClosingStart.
        /// </summary>
        public DateTime ClosingStart { get; set; }

        /// <summary>
        /// Gets or sets ClosingStop.
        /// </summary>
        public DateTime ClosingStop { get; set; }

        /// <summary>
        /// Gets or sets ContinuousTradingStart.
        /// </summary>
        public DateTime ContinuousTradingStart { get; set; }

        /// <summary>
        /// Gets or sets ContinuousTradingStop.
        /// </summary>
        public DateTime ContinuousTradingStop { get; set; }

        /// <summary>
        /// Gets or sets ID.
        /// </summary>
        public Guid ID { get; set; }

        /// <summary>
        /// Gets or sets IntradayAuctionStart.
        /// </summary>
        public DateTime IntradayAuctionStart { get; set; }

        /// <summary>
        /// Gets or sets IntradayAuctionStop.
        /// </summary>
        public DateTime IntradayAuctionStop { get; set; }

        /// <summary>
        /// Gets or sets OpeningStart.
        /// </summary>
        public DateTime OpeningStart { get; set; }

        /// <summary>
        /// Gets or sets OpeningStop.
        /// </summary>
        public DateTime OpeningStop { get; set; }

        /// <summary>
        /// Gets or sets Path.
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// Gets or sets PreClosingStart.
        /// </summary>
        public DateTime PreClosingStart { get; set; }

        /// <summary>
        /// Gets or sets PreClosingStop.
        /// </summary>
        public DateTime PreClosingStop { get; set; }

        /// <summary>
        /// Gets or sets PreOpeningStart.
        /// </summary>
        public DateTime PreOpeningStart { get; set; }

        /// <summary>
        /// Gets or sets PreOpeningStop.
        /// </summary>
        public DateTime PreOpeningStop { get; set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// The is closed.
        /// </summary>
        /// <param name="time">
        /// The time.
        /// </param>
        /// <returns>
        /// The is closed.
        /// </returns>
        public bool IsClosed(DateTime time)
        {
            return time > this.ClosingStop;
        }

        /// <summary>
        /// The is closing.
        /// </summary>
        /// <param name="time">
        /// The time.
        /// </param>
        /// <returns>
        /// The is closing.
        /// </returns>
        public bool IsClosing(DateTime time)
        {
            return time >= this.ClosingStart && time <= this.ClosingStop;
        }

        // The closing match is performed.

        /// <summary>
        /// The is continuous trading.
        /// </summary>
        /// <param name="time">
        /// The time.
        /// </param>
        /// <returns>
        /// The is continuous trading.
        /// </returns>
        public bool IsContinuousTrading(DateTime time)
        {
            return time >= this.ContinuousTradingStart && time <= this.ContinuousTradingStop;
        }

        /// <summary>
        /// The is intraday auction.
        /// </summary>
        /// <param name="time">
        /// The time.
        /// </param>
        /// <returns>
        /// The is intraday auction.
        /// </returns>
        public bool IsIntradayAuction(DateTime time)
        {
            return time >= this.IntradayAuctionStart && time <= this.IntradayAuctionStop;
        }

        /// <summary>
        /// The is opened.
        /// </summary>
        /// <param name="time">
        /// The time.
        /// </param>
        /// <returns>
        /// The is opened.
        /// </returns>
        public bool IsOpened(DateTime time)
        {
            return time >= this.PreOpeningStart;
        }

        /// <summary>
        /// The is opening.
        /// </summary>
        /// <param name="time">
        /// The time.
        /// </param>
        /// <returns>
        /// The is opening.
        /// </returns>
        public bool IsOpening(DateTime time)
        {
            return time >= this.OpeningStart && time <= this.OpeningStop;
        }

        /// <summary>
        /// The is pre closing.
        /// </summary>
        /// <param name="time">
        /// The time.
        /// </param>
        /// <returns>
        /// The is pre closing.
        /// </returns>
        public bool IsPreClosing(DateTime time)
        {
            return time >= this.PreClosingStart && time <= this.PreClosingStop;
        }

        /// <summary>
        /// The is pre opening.
        /// </summary>
        /// <param name="time">
        /// The time.
        /// </param>
        /// <returns>
        /// The is pre opening.
        /// </returns>
        public bool IsPreOpening(DateTime time)
        {
            return time >= this.PreOpeningStart && time <= this.PreOpeningStop;
        }

        /// <summary>
        /// The to string.
        /// </summary>
        /// <returns>
        /// The to string.
        /// </returns>
        public override string ToString()
        {
            return this.Path;
        }

        #endregion
    }
}